﻿using EcoSystem.Enums;
using System;
using System.Collections.Generic;
using System.Linq;

namespace EcoSystem
{
    /// <summary>
    /// 键值对请求参数定义类
    /// </summary>
    [Serializable]
    public class DictionaryRequestInputDto : Dictionary<string, object>
    {
        public DictionaryRequestInputDto()
        {

        }

        public float GetFloat(string key)
        {
            return Convert.ToSingle(this[key]);
        }

        public double GetDouble(string key)
        {
            return Convert.ToDouble(this[key]);
        }

        public decimal GetDecimal(string key)
        {
            return Convert.ToDecimal(this[key]);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public int GetInt(string key)
        {
            return Convert.ToInt32(this[key]);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool GetBoolean(string key)
        {
            return Convert.ToBoolean(this[key]);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public DateTime GetDateTime(string key)
        {
            return Convert.ToDateTime(this[key]);
        }

        /// <summary>
        /// 获取返回记录数
        /// </summary>
        /// <returns></returns>
        public int MaxResultCount()
        {
            if (this.ContainsKey("maxResultCount"))
            {
                return this.GetInt("maxResultCount");
            }
            if (this.ContainsKey("MaxResultCount"))
            {
                return this.GetInt("MaxResultCount");
            }
            return 20;
        }

        /// <summary>
        /// 获取跳过记录数
        /// </summary>
        /// <returns></returns>
        public int SkipCount()
        {
            if (this.ContainsKey("skipCount"))
            {
                return this.GetInt("skipCount");
            }
            if (this.ContainsKey("SkipCount"))
            {
                return this.GetInt("SkipCount");
            }
            return 0;
        }

        /// <summary>
        /// 获取排序字段集合
        /// </summary>
        /// <returns></returns>
        public SqlOrderBy[] Sorting()
        {
            var sortings = new List<SqlOrderBy>();
            string orderBy = "";
            if (this.ContainsKey("sorting"))
            {
                orderBy = Convert.ToString(this["sorting"]);
            }
            if (this.ContainsKey("Sorting"))
            {
                orderBy = Convert.ToString(this["Sorting"]);
            }
            if (string.IsNullOrEmpty(orderBy))
            {
                sortings.Add(new SqlOrderBy() { Name = "Id", Sorting = SqlSorting.DESC });
            }
            if (orderBy.IndexOf(",") > 0)
            {
                var orderBys = orderBy.Split(',');
                foreach (var byItem in orderBys)
                {
                    sortings.Add(CreateOrderBy(byItem));
                }
            }
            else
            {
                sortings.Add(CreateOrderBy(orderBy));
            }
            return sortings.ToArray();
        }

        /// <summary>
        /// 创建排序处理
        /// </summary>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        private SqlOrderBy CreateOrderBy(string orderBy)
        {
            if (orderBy.Trim().IndexOf(" ") > 0)
            {
                var values = orderBy.Split(" ");
                var isDesc = string.Equals(values[1], "desc", StringComparison.OrdinalIgnoreCase);
                return new SqlOrderBy()
                {
                    Name = values[0],
                    Sorting = isDesc ? SqlSorting.DESC : SqlSorting.ASC
                };
            }
            else
            {
                return new SqlOrderBy()
                {
                    Name = orderBy,
                    Sorting = SqlSorting.ASC
                };
            }
        }
    }
}